package com.yztc.youbadbad.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.bean.homepage.ProductBean;
import com.yztc.youbadbad.helper.BaseAdapterHelper;
import com.yztc.youbadbad.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Tiffany.V.Chou on 2016/11/20.
 */

public class ShoppingCarAdapter extends BaseAdapterHelper<ProductBean> {
    private IItemCheckedboxChange iItemCheckedboxChange;

    public interface IItemCheckedboxChange{
        void itemCheckboxChange(int state, double item, int delete);
    }

    public ShoppingCarAdapter(Context context, List<ProductBean> list,
                              IItemCheckedboxChange iItemCheckedboxChange) {
        super(context, list);
        this.iItemCheckedboxChange = iItemCheckedboxChange;
    }

    @Override
    public View getItemView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.item_shoppingcar, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }


        final ProductBean product = mList.get(position);
        vh.storePrice.setText(product.getStore_price()+"");//价格
        vh.name.setText(product.getName());//描述
        vh.num.setText(product.getNum()+"");//数量
        vh.checkboxItem.setChecked(product.isCheck());

        vh.checkboxItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                product.setCheck(!product.isCheck());
                notifyDataSetChanged();
                iItemCheckedboxChange.itemCheckboxChange(0, product.getStore_price()*product.getNum(), 0);//传递给调用者

            }
        });

        Glide.with(mContext).load(product.getProduct_image()).into(vh.productImage);//图片

        final ViewHolder finalVh = vh;
        vh.add.setOnClickListener(new View.OnClickListener() {//加
            @Override
            public void onClick(View v) {
                int newNum = product.getNum()+1;
                finalVh.num.setText(newNum+"");//加一
                product.setNum(newNum);
                iItemCheckedboxChange.itemCheckboxChange(1, product.getStore_price(), 1);//传递给调用者
                product.update(product.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Log.i("bmob","更新成功");
                        }else{
                            Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                        }
                    }
                });
            }
        });


        vh.substact.setOnClickListener(new View.OnClickListener() {//加
            @Override
            public void onClick(View v) {
                if (product.getNum() == 1){
                    ToastUtils.showToast(mContext, "最小数量为1");
                }else {
                    int newNum = product.getNum()-1;
                    finalVh.num.setText(newNum+"");//加一
                    product.setNum(newNum);
                    iItemCheckedboxChange.itemCheckboxChange(2, product.getStore_price(), 1);//传递给调用者
                    product.update(product.getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                                Log.i("bmob","更新成功");
                            }else{
                                Log.i("bmob","更新失败："+e.getMessage()+","+e.getErrorCode());
                            }
                        }
                    });
                }
            }
        });

        vh.delete.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                product.delete(product.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        iItemCheckedboxChange.itemCheckboxChange(2, product.getStore_price()*product.getNum(), product.getNum());
                        removeItem(position);
                    }
                });
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.checkbox_item)
        CheckBox checkboxItem;
        @BindView(R.id.product_image)
        ImageView productImage;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.store_price)
        TextView storePrice;
        @BindView(R.id.substact)
        TextView substact;
        @BindView(R.id.num)
        TextView num;
        @BindView(R.id.add)
        TextView add;
        @BindView(R.id.delete)
        TextView delete;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
